package com.yonyou.cyxdms.basedata.service.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.*;

public class ZipUtils {
    /**
     * 使用gzip进行压缩
     */
    public static String gzip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip进行解压缩</p>
     *
     * @param compressedStr
     * @return
     */
    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

    /**
     * 使用zip进行压缩
     *
     * @param str 压缩前的文本
     * @return 返回压缩后的文本
     */
    public static final String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 使用zip进行解压缩
     *
     * @param compressedStr 压缩后的文本
     * @return 解压后的字符串
     */
    public static final String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }

    public static void main(String[] args) throws IOException {
////        String strOld= FileUtils.readFileToString(new File("D:/test.txt"),"utf-8");
//        String strOld ="https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,https://blog.csdn.net/RaoSK/article/details/121519355,";
//        String gzip = zip(strOld);
//        System.out.printf("old:=%d zip new:%d\r\n",strOld.length(),gzip.length());
//        System.out.printf("new:=%s ",gzip);
//
//        System.out.printf("\r\n...... ",gzip);
//        String strOld1 ="www.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdlwww.baidu.com1234567890abcdefeaghodadfsdfasdf.sdf.sadfmsmadmfmsdmfmasmdfsdkfkaskdfsdfmamwefkksafs.df..asd.f.asdfwkefkkejuuwq@##&&**!^&!*!((!))!))!)!))!)!(*&^%%$%&!*!***!*!*!*!**!*!*!*!**!******!&&^^JJSSYDHDJoadsp;fddlslklsdlkflksdl";
//        String gzip1 = zip(strOld1);
//        System.out.printf("old:=%d zip new:%d\r\n",strOld1.length(),gzip1.length());
//        System.out.printf("new:=%s ",gzip1);
        System.out.println(unzip("UEsDBBQACAgIAIR6W1UAAAAAAAAAAAAAAAABAAAAMO2XXU8aQRSG/8teE7O7Skm4U6NNoxJT0cQ0\n" +
                "vSCwFyRUjMCV8QIaWrfFRUFrCRCL8mm0FGsbXAX+DDOze+Vf6FJqk3YOK8TU8rF3ZJjnPXvOnH3P\n" +
                "zibjcvgdjPXFJuNd97u9a9Nel8BYmaU5lmNMv9ZsjletNbRzgHb3lFpeqdVQOEz2v+GUzGyZaJSn\n" +
                "0GikG3R5lY6KDz/N2KfBvVQYcOMwpfIUFG3WC8pJWM19wekMCFHqJHSuXu/dww1P3WxLtGizuoOq\n" +
                "efzuDGUv8cc6yQIPo3F0HvdBo3BIZjBF5bJAPldI8gNIUMHa29HVV5j4F43wbAoUReKOlqeyXUGh\n" +
                "IhFLIEfX6k+IJGNAA82xlr+58TGz8r6CylX7rF0t1dXS92Yjg4Pldj+hShSS4anH5vix8bZOlyJG\n" +
                "qxhjY3gdaeDHxjwQrGVNwSjJX2v5q8G4cp7vhNLxuuCMvhh5a/gPXbcwC4csX+HyvnqcUsQLCOKo\n" +
                "QYqOi0QStca7vdmeYi23N2LfHNWgDr1HPZtFS0f7qUZxugESkLHqEKNgcYPabMatbAAGhG0R7q5M\n" +
                "BKclrWOUehy9lbGUQ9ETkDZTnXaHcs3qKcevoFiE137xPLuiL7bKTYLmtCthKYbkApIP1NdFnA3r\n" +
                "XMgm52GDy5bI6SG4fYI6jXARJ8qdiAfX60nHevG/6zXxaPXSNOh2vBNooT81dARmOLAeJHmBIjKK\n" +
                "lpXCGxCi3ztdYvm5ftlRSntTG0TO4/QRjoTUGHDJ1zRoW+xFoC8K3heVGDLXePChGLYzCLZjswFj\n" +
                "+0jGqTM1mMNSQY3lUCQBcRxLlS+eJmKiE2EYnGFwhsEZBmd8Vxm202MlesvipYkRPI51n+BacHs8\n" +
                "bp/g9K65fIyVNTEbgi/g8beFeVZb8AWcTsGn/enfCAhbPwBQSwcIowP1m7ACAAAaIAAA\n"));
    }
}
